import Mock from 'mockjs'

// 生成权限树形数据
const generatePermissions = () => {
  const permissions = [
    {
      id: 1,
      name: '系统管理',
      code: 'SYSTEM_MANAGE',
      type: 'menu',
      path: '/system',
      icon: 'Setting',
      sort: 1,
      status: '1',
      remark: '系统管理模块',
      createTime: '2024-01-01 00:00:00',
      children: [
        {
          id: 11,
          name: '用户管理',
          code: 'USER_MANAGE',
          type: 'menu',
          path: '/user',
          icon: 'User',
          sort: 1,
          status: '1',
          remark: '用户管理页面',
          createTime: '2024-01-01 00:00:00'
        },
        {
          id: 12,
          name: '角色管理',
          code: 'ROLE_MANAGE',
          type: 'menu',
          path: '/role',
          icon: 'UserFilled',
          sort: 2,
          status: '1',
          remark: '角色管理页面',
          createTime: '2024-01-01 00:00:00'
        },
        {
          id: 13,
          name: '权限管理',
          code: 'PERMISSION_MANAGE',
          type: 'menu',
          path: '/permission',
          icon: 'Lock',
          sort: 3,
          status: '1',
          remark: '权限管理页面',
          createTime: '2024-01-01 00:00:00'
        }
      ]
    }
  ]
  
  return permissions
}

let permissionList = generatePermissions()

export default function() {
  // 获取权限列表
  Mock.mock(/\/api\/permission\/list/, 'get', () => {
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: permissionList,
        tree: permissionList
      }
    }
  })

  // 创建权限
  Mock.mock('/api/permission/create', 'post', (options) => {
    const permissionData = JSON.parse(options.body)
    const newPermission = {
      id: Date.now(),
      ...permissionData,
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    }
    
    permissionList.push(newPermission)
    
    return {
      code: 200,
      message: '创建成功',
      data: newPermission
    }
  })

  // 更新权限
  Mock.mock(/\/api\/permission\/update\/\d+/, 'put', () => {
    return {
      code: 200,
      message: '更新成功',
      data: null
    }
  })

  // 删除权限
  Mock.mock(/\/api\/permission\/delete\/\d+/, 'delete', () => {
    return {
      code: 200,
      message: '删除成功',
      data: null
    }
  })

  // 获取权限详情
  Mock.mock(/\/api\/permission\/detail\/\d+/, 'get', () => {
    return {
      code: 200,
      message: '获取成功',
      data: permissionList[0]
    }
  })
} 